<template>
  <div class="container">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item v-if="!$route.query.id" label="所属学科" prop="subjectID">
        <el-select v-model="ruleForm.subjectID" placeholder="请选择" style="width:80%">
          <el-option
            v-for="item in simpleList"
            :key="item.id"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="ruleForm.directoryName" placeholder="请输入目录名称" style="width:80%" />
      </el-form-item>
      <el-form-item>
        <el-button @click="$emit('close')">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { getSimple, add, update } from '@/api/hmmm/directorys'
export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    curRow: {
      type: Object,
      required: true
    },
    dirName: {
      type: Array,
      required: true
    }
  },
  data() {
    var validateName = (rule, value, callback) => {
      console.log(this.dirName, '11')
      console.log(value, 'value')
      console.log(rule, 'rule')
      const dirName = this.dirName.map(item => item.directoryName)
      if (dirName.includes(value)) {
        callback(new Error('名字' + value + '已经被占用了'))
      } else {
        callback()
      }
      if (this.isEdit) {
        this.dirName.filter(item => !item.directoryName)
      }
    }
    return {
      // 目录添加初始值
      ruleForm: {
        subjectID: '',
        directoryName: ''
      },
      simpleList: [], // 存储简单目录列表
      // 校验规则
      rules: {
        directoryName: [
          { required: true, message: '请输入项目名称', trigger: 'change' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadSimple()
  },
  methods: {
    // 获取简单目录列表
    async loadSimple() {
      try {
        // 1.发送请求
        if (this.$route.query.id) {
          this.ruleForm.subjectID = this.$route.query.id
        }
        const res = await getSimple({ data: { subjectName: this.subName }})
        console.log(res, '简单列表')
        // 2.存储目录列表
        this.simpleList = res.data
        console.log(this.simpleList)
      } catch (error) {
        // 3.发送错误信息
        this.$message.error(error.message)
      }
    },
    // 确定触发事件
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        console.log(formName)
        console.log(valid)
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 目录添加
    async doAdd() {
      try {
        // 1.发送请求
        const res = await add(this.ruleForm)
        console.log(res, '添加')
        // 2.通知父组件
        this.$emit('success')
        this.id = res.data.id
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // 修改目录
    async doEdit() {
      try {
        const res = await update(this.ruleForm)
        console.log(res, '修改')
        this.$emit('success')
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    fetchRow() {
      this.ruleForm = this.curRow
    }

  }
}
</script>

<style scoped lang='scss'>

</style>
